package com.tf_cut.backend.util;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.alibaba.fastjson.JSON;
import com.tf_cut.backend.platform.vo.GrantDetailVo;
import com.tf_cut.backend.seller.vo.RegisterUploadDetailVo;
import com.tf_cut.backend.seller.vo.SellerOrderVo;
import com.tf_cut.backend.seller.vo.SellerUploadDetailVo;

public class ExcelToTxt {

	public static String filePath = "/home/ubuntu/data/uploadFile/";

	public static String uploadPath = "mobile/txt/";

	/**
	 * 发放txt
	 * 
	 * @param sellerOrderVo
	 * @param GrantDetailVo
	 * @return
	 */
	public static String excelToTxt(SellerOrderVo sellerOrderVo, List<GrantDetailVo> GrantDetailVo) {
		String path = filePath + sellerOrderVo.getOrderId() + ".txt";
		String columns[] = { "isvOrgId", "merchantId", "batchNo", "outDetailId", "relateOutDetailId", "amount", "ccy",
				"type", "payeeName", "payeeCertNo", "payeeAccountType", "payeeCardNo", "payeeBranchName",
				"payeeContactLine", "payeeMobile", "remark" };
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < columns.length; i++) {
			sb.append(columns[i] + ",");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("\r\n");
		for (int i = 0; i < GrantDetailVo.size(); i++) {
			sb.append(MyConfig.isvOrgId + ",");
			sb.append(sellerOrderVo.getMerchantId() + ",");
			sb.append(sellerOrderVo.getOrderId() + ",");
			sb.append(GrantDetailVo.get(i).getId() + ",");
			sb.append("" + ",");
			double sum = GrantDetailVo.get(i).getPayAmount().doubleValue() * 100;
			sb.append((int) sum + ",");
			sb.append("156" + ",");
			sb.append("02" + ",");
			sb.append(GrantDetailVo.get(i).getPayeeName() + ",");
			sb.append(GrantDetailVo.get(i).getCardCode() + ",");
			sb.append("01" + ",");
			sb.append(GrantDetailVo.get(i).getPayeeAccount() + ",");
			sb.append(GrantDetailVo.get(i).getPayeeAccountBankName() + ",");
			sb.append("" + ",");
			sb.append(GrantDetailVo.get(i).getPayeeMobile() + ",");
			sb.append(GrantDetailVo.get(i).getPayeeRemark());
			sb.append("\r\n");
		}
		try {
			WriteToFile(sb.toString(), path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("*************EXCEL转成TXT格式成功*************");
		return path;
	}

	/**
	 * 服务费txt
	 * 
	 * @param sellerOrderVo
	 * @param GrantDetailVo
	 * @return
	 */
	public static String excelToSellerTxt(RegisterUploadDetailVo egisterUploadDetailVo,SellerUploadDetailVo sellerUploadDetailVo) {
		String path = filePath + sellerUploadDetailVo.getId() + ".txt";
		String columns[] = { "isvOrgId", "merchantId", "batchNo", "outDetailId", "relateOutDetailId", "amount", "ccy",
				"type", "payeeName", "payeeCertNo", "payeeAccountType", "payeeCardNo", "payeeBranchName",
				"payeeContactLine", "payeeMobile", "remark" };
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < columns.length; i++) {
			sb.append(columns[i] + ",");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("\r\n");
		
		sb.append(MyConfig.isvOrgId + ",");
		sb.append(egisterUploadDetailVo.getMerchantId() + ",");
		sb.append(sellerUploadDetailVo.getId() + ",");
		sb.append(sellerUploadDetailVo.getId() + ",");
		sb.append("" + ",");
		double sum = sellerUploadDetailVo.getServiceAmount() * 100;
		sb.append((int) sum + ",");
		sb.append("156" + ",");
		sb.append("01" + ",");
		sb.append("江西佰和信息科技有限公司" + ",");
		sb.append("" + ",");
		sb.append("02" + ",");
		sb.append("8888888964151680" + ",");
		sb.append("浙江网商银行股份有限公司" + ",");
		sb.append("323331000001" + ",");
		sb.append("" + ",");
		sb.append("");
		
		sb.append("\r\n");
		try {
			WriteToFile(sb.toString(), path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("*************EXCEL转成TXT格式成功*************");
		return path;
	}

	// 读取excel
	public static Workbook readExcel(String filePath) {
		Workbook wb = null;
		if (filePath == null) {
			return null;
		}
		String extString = filePath.substring(filePath.lastIndexOf("."));
		InputStream is = null;
		try {
			is = new FileInputStream(filePath);
			if (".xls".equals(extString)) {
				return wb = new HSSFWorkbook(is);
			} else if (".xlsx".equals(extString)) {
				return wb = new XSSFWorkbook(is);
			} else {
				return wb = null;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return wb;
	}

	/**
	 * 生成文件
	 * 
	 * @param str
	 * @param filePath
	 * @throws IOException
	 */

	public static void WriteToFile(String str, String filePath) throws IOException {
		BufferedWriter bw = null;
		try {
			FileOutputStream out = new FileOutputStream(filePath, false);// true,表示:文件追加内容，不重新生成,默认为false
			bw = new BufferedWriter(new OutputStreamWriter(out, "GBK"));
			bw.write(str += "\r\n");// 换行
			bw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bw.close();
		}
	}

	public static String excelOut(SellerOrderVo sellerOrderVo, List<GrantDetailVo> GrantDetailVo) {
		String path = "E:\\tes\\tes.txt";
		String columns[] = { "isvOrgId", "merchantId", "batchNo", "outDetailId", "relateOutDetailId", "amount", "ccy",
				"type", "payeeName", "payeeCertNo", "payeeAccountType", "payeeCardNo", "payeeBranchName",
				"payeeContactLine", "payeeMobile", "remark" };

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < columns.length; i++) {
			sb.append(columns[i] + ",");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("\r\n");

		for (int i = 0; i < GrantDetailVo.size(); i++) {
			sb.append("11233" + ",");
			sb.append(sellerOrderVo.getMerchantId() + ",");
			sb.append(sellerOrderVo.getOrderId() + ",");
			sb.append(GrantDetailVo.get(i).getId() + ",");
			sb.append("" + ",");
			double sum = GrantDetailVo.get(i).getPayAmount().doubleValue() * 100;
			sb.append((int) sum + ",");
			sb.append("156" + ",");
			sb.append("02" + ",");
			sb.append(GrantDetailVo.get(i).getPayeeName() + ",");
			sb.append(GrantDetailVo.get(i).getCardCode() + ",");
			sb.append("01" + ",");
			sb.append(GrantDetailVo.get(i).getPayeeAccount() + ",");
			sb.append(GrantDetailVo.get(i).getPayeeAccountBankName() + ",");
			sb.append("" + ",");
			sb.append(GrantDetailVo.get(i).getPayeeMobile() + ",");
			sb.append(GrantDetailVo.get(i).getPayeeRemark());
			sb.append("\r\n");
		}

		try {
			WriteToFile(sb.toString(), path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("*************EXCEL转成TXT格式成功*************");

		return path;
	}

}
